<?php
$materie_all = $materia->getMaterie();

/*
  echo '<pre>';
  print_r($materie_all);
  echo '</pre>';
 */
$materia_result = null;

if (isset($_GET['mid'])) {
    $materia_result = $materia->getMateriaById($_GET['mid']);
}

if (isset($_REQUEST['upload_file'])) {
    $limit_size = 150000;
    $upload_error = array();
    $materia_tmp = $materia->getMateriaById($_POST['materia_id']);
    $unique_id = md5(uniqid(microtime(), true)) . '/';
    $percorso_statico = 'static/upload/';

    $percorso_cartella = $percorso_statico . $materia_tmp->name . '/' . $unique_id;
    $file_url = $percorso_cartella . $_FILES['file']['name'];
    
    if (!is_dir($percorso_cartella)) {
        mkdir($percorso_cartella);
    }
    // Store upload file size in $file_size
    $file_size = $_FILES['file']['size'];

    if ($file_size >= $limit_size) {
        $upload_error['size'] = 'Limite dimensione file: 15MB';
    }
    if (count($upload_error) > 0) {
        foreach ($upload_error as $error) {
            echo 'Errore: ' . $error . '<br />';
        }
    }if (!move_uploaded_file($_FILES['file']['tmp_name'], $file_url)) {
        $msg = "<p>Errore nel caricamento del file!!</p>";
    } else {
        $add_attachment_result = Attachment::addAttachment($_FILES["file"]["name"], date("Y-m-d H:i:s", time()), $_POST['description'], $user->id, $materia_tmp->id_materia, ($_FILES["file"]["size"] / 1024) . " KB", $file_url);
        if (!$add_attachment_result) {
            echo 'Errore nell\'caricamento del file sul DB';
        } else {
            header("Location: index.php?p=materie&mid=" . $_GET['mid']);
        }
    }
}

if (isset($_GET['aid'])) {
    $remove_attachment_result = Attachment::deleteAttachment($_GET['aid']);
    if (!$remove_attachment_result) {
        echo 'Errore durante l\'eliminazione del file';
    } else {
        header("Location: index.php?p=materie&mid=" . $_GET['mid']);
    }
}
?>

<?php if ($user->isLogged()) { ?>
    <div style="padding:10px">
        <span class="titletext"><?php if (isset($materia_result)) {
        echo $materia_result->name;
    } else {
        echo "Materie";
    } ?></span>
    </div>
    <div class="bodytext" style="padding:12px;" align="justify">
        <?php if (!isset($materia_result)) { ?>
            <?php foreach ($materie_all as $singola_materia) { ?>	
                <strong><a href="?p=materie&mid=<?php echo $singola_materia->id_materia; ?>"><?php echo $singola_materia->name; ?></a></strong><br />
                    <?php echo $singola_materia->description; ?><br /><br />
                <?php } ?>
    <?php } else { ?>
            <ul>
                <strong><?php echo 'Totale allegati: ' . count($materia_result->attachment); ?></strong><br /><br /><br />
                <?php foreach ($materia_result->attachment as $file) { ?>
                    <span class="smallredtext"><?php echo Util::getDate($file->date); ?></span><br />
                    <span class=""><a href="<?php echo $file->file_url; ?>"><?php echo '<strong>' . $file->name . '</strong>'; ?></a><?php echo ' [' . $file->weight . 'KB] <br /> Caricato da: ' . $user->getUser($file->id_user); ?></span><br />
                <?php if ($user->isAdmin()) { ?><a href="?p=materie&mid=<?php echo $_GET['mid']; ?>&aid=<?php echo $file->id_attachment; ?>">Elimina</a><?php } ?>
                    <br /><br />
            <?php } ?>

            </ul>
    <?php } ?>

    <?php if (isset($_GET['mid'])) { ?>                        
            <div id="uploadbox">
                <form action="#" method="post" enctype="multipart/form-data">
                    <label for="file">Filename:</label><br />
                    <input type="file" name="file" id="file" />
                    <br />
                    <textarea cols="25" rows="10" name="description"></textarea>
                    <input type="submit" name="upload_file" value="Submit" />
                    <input type="hidden" name="materia_id" value="<?php if (isset($materia_result)) {
            echo $materia_result->id_materia;
        } ?>">
                </form>
            </div>
    <?php } ?>
    </div>

    </div>
<?php
} else {
    header("Location: index.php?p=home");
}
?>